# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=binutils
_base_ver=2.35
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
# replaces="${MINGW_PACKAGE_PREFIX}-${_realname}=${_base_ver}"
pkgver=2.35.1.r102542.bbbeba06dd9
pkgrel=1
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgdesc="A set of programs to assemble and manipulate binary and object files"
arch=('any')
url="https://www.gnu.org/software/binutils/"
license=('GPL')
#groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-libiconv" "${MINGW_PACKAGE_PREFIX}-zlib")
#checkdepends=('dejagnu' 'bc')
makedepends=("${MINGW_PACKAGE_PREFIX}-libiconv" "${MINGW_PACKAGE_PREFIX}-zlib" "git")
options=('staticlibs' '!distcc' '!ccache' 'debug' '!strip')
#install=binutils.install
source=("${_realname}"::"git+https://sourceware.org/git/binutils-gdb.git#branch=${_realname}-${_base_ver//./_}-branch"
        0001-enable-gold-on.mingw32.patch
        0002-check-for-unusual-file-harder.patch
        0010-bfd-Increase-_bfd_coff_max_nscns-to-65279.patch
        0020-binutils_2.31_mkdtemp_impl.patch
        0110-binutils-mingw-gnu-print.patch
        0120-windres-handle-spaces.patch)
sha256sums=('SKIP'
            '93296b909e1a4f9d8a4bbe2437aafa17ca565ef6642a9812b0360c05be228c9d'
            '2c99345fc575c3a060d6677537f636c6c4154fac0fde508070f3b6296c1060d4'
            '604e76e0f702ced493ee22aa3c1768b4776b2008a7d70ae0dd35fe5be3522141'
            '34ba6c001ff7f95ae1da0619c73130112b76d0d2a31bb8a00602eb22f1f84cb8'
            '76658ef1bb8c5fc3fe6c26e2b5dd9ee0f1d12661988c0c65562b0a3e2d32ae1f'
            '86ae90d997e986a54aaebb5251f3a71800b0c5c3f5b57b9094a42995e9f5c478')

# Declare global variables; begin with underscore to avoid name conflicts
_git_base_commit=

pkgver() {
  cd "$srcdir/$_realname"

  _bfd_version=$(head -n 3 bfd/version.m4 | grep 'BFD_VERSION' | sed -e 's/.* //' | tr -d '[])' | tr '\n' '.' | sed 's/.$/\n/')

  printf "%s.r%s.%s" "${_bfd_version}" "$(git rev-list --count ${_git_base_commit})" "$(git rev-parse --short ${_git_base_commit})"
}

prepare() {
  cd ${srcdir}/${_realname}

  _git_base_commit=$(git rev-parse HEAD)

  git am "${srcdir}"/0010-bfd-Increase-_bfd_coff_max_nscns-to-65279.patch
  patch -p1 -i "${srcdir}"/0001-enable-gold-on.mingw32.patch
  patch -p1 -i "${srcdir}"/0002-check-for-unusual-file-harder.patch
  patch -p1 -i "${srcdir}"/0020-binutils_2.31_mkdtemp_impl.patch
  patch -p1 -i "${srcdir}"/0110-binutils-mingw-gnu-print.patch
  patch -p1 -i "${srcdir}"/0120-windres-handle-spaces.patch
  rm -rf gdb gdbserver gdbsupport readline libdecnumber sim
}

build() {
  if [ "${CARCH}" = "x86_64" ]; then
    local _conf='--enable-64-bit-bfd'
  else
    local _conf=''
    LDFLAGS+=" -Wl,--large-address-aware"
  fi

  if check_option "debug" "y"; then
    CFLAGS+=" -O0"
  fi

  cd $srcdir
  rm -rf build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}
  ../${_realname}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --disable-werror \
    --enable-lto \
    --with-sysroot=${MINGW_PREFIX} \
    --with-libiconv-prefix=${MINGW_PREFIX} \
    $_conf \
    --enable-nls \
    --disable-rpath \
    --disable-multilib \
    --enable-install-libiberty \
    --enable-plugins \
    --enable-gold \
    --disable-shared

  make
}

check() {
  cd ${srcdir}/build-${MINGW_CHOST}

  # unset LDFLAGS as testsuite makes assumptions about which ones are active
  # do not abort on errors - manually check log files
  make LDFLAGS="" -k check || true
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  mkdir -p ${pkgdir}${MINGW_PREFIX}/{lib,include}/${_realname}
  mkdir -p ${pkgdir}${MINGW_PREFIX}/${MINGW_CHOST}/include
  cp ${pkgdir}${MINGW_PREFIX}/include/ansidecl.h ${pkgdir}${MINGW_PREFIX}/${MINGW_CHOST}/include/
  mv ${pkgdir}${MINGW_PREFIX}/include/*.h ${pkgdir}${MINGW_PREFIX}/include/${_realname}
  mv ${pkgdir}${MINGW_PREFIX}/lib/*.a ${pkgdir}${MINGW_PREFIX}/lib/${_realname}

  # Remove unwanted files
  find ${pkgdir}${MINGW_PREFIX}/share/locale -type f -iname "bfd.*" -o -iname "opcodes.*" | xargs -rtl1 rm
  # rm -rf ${pkgdir}${MINGW_PREFIX}/share

  # Add some useful headers
  #install -m644 ${srcdir}/${_realname}-${pkgver}/include/libiberty.h ${pkgdir}${MINGW_PREFIX}/include
  #install -m644 ${srcdir}/${_realname}-${pkgver}/include/demangle.h ${pkgdir}${MINGW_PREFIX}/include

  find  ${pkgdir}${MINGW_PREFIX}/share -type f -iname "opcodes.mo" -o -iname "bfd.mo" | xargs -rtl1 rm
}
